package com.tarena.lbs.user.dao.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.common.utils.AuthenticationContextUtils;
import com.tarena.lbs.user.dao.mapper.UserGroupMapper;
import com.tarena.lbs.user.dao.repository.AdminRepository;
import com.tarena.lbs.user.dao.repository.UserGroupRepository;
import com.tarena.lbs.user.dao.repository.UserRepository;
import com.tarena.lbs.user.pojo.param.UserGroupParam;
import com.tarena.lbs.user.pojo.po.AdminPO;
import com.tarena.lbs.user.pojo.po.UserGroupPO;
import com.tarena.lbs.user.pojo.po.UserPO;
import com.tarena.lbs.user.pojo.query.UserGroupQuery;
import com.tarena.lbs.user.pojo.vo.UserGroupVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
@Slf4j
@Repository
public class UserGroupRepositoryImpl implements UserGroupRepository {
    @Autowired
    private UserGroupMapper userGroupMapper;
    @Autowired
    private AdminRepository adminRepository;

    @Override
    public List<UserGroupPO> selectGrouplist() {
        /*获取当前登录商家的用户id*/
        Integer id = AuthenticationContextUtils.get().getId();
        log.info("*****当前用户id:"+id);
        /*根据登录商家的用户id获取关联的商家id*/
        AdminPO adminPO = adminRepository.getAdminById(id);
        Integer businessId = adminPO.getBusinessId();
        log.info("*****关联的商家id:"+businessId);
        /*根据商家id查询用户分组列表*/
        QueryWrapper<UserGroupPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("business_id",businessId);
        UserGroupQuery userGroupQuery = new UserGroupQuery();
        userGroupQuery.setBusinessId(businessId);
        userGroupQuery.setPageNo(2);
        PageHelper.startPage(userGroupQuery.getPageNo(), userGroupQuery.getPageSize());
        List<UserGroupPO> userGroupPOS = userGroupMapper.selectList(queryWrapper);
        return userGroupPOS;
    }

    @Override
    public void save(UserGroupParam param) {
        /*获取当前登录商家的用户id*/
        Integer id = AuthenticationContextUtils.get().getId();
        log.info("*****当前用户id:"+id);
        /*根据登录商家的用户id获取关联的商家id*/
        AdminPO adminPO = adminRepository.getAdminById(id);
        Integer businessId = adminPO.getBusinessId();
        log.info("*****关联的商家id:"+businessId);

        UserGroupPO userGroupPO = new UserGroupPO();
        BeanUtils.copyProperties(param,userGroupPO);
        userGroupPO.setBusinessId(businessId);

        userGroupMapper.insert(userGroupPO);
    }

}
